package com.ruoyi.technicianAudit.service.impl;

import cn.hutool.core.util.IdcardUtil;
import com.ruoyi.broker.domain.Broker;
import com.ruoyi.broker.mapper.BrokerMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.md5.MD5Utils;
import com.ruoyi.config.util.address.AddressUtil;
import com.ruoyi.config.util.address.WSSsdrAddress;
import com.ruoyi.config.util.gaode.GaoDeUtil;
import com.ruoyi.technician.domain.Technician;
import com.ruoyi.technician.mapper.TechnicianMapper;
import com.ruoyi.technician.service.ITechnicianService;
import com.ruoyi.technicianAudit.domain.Recruitment;
import com.ruoyi.technicianAudit.mapper.RecruitmentMapper;
import com.ruoyi.technicianAudit.service.IRecruitmentService;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 技师申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class RecruitmentServiceImpl implements IRecruitmentService {
    @Autowired
    private RecruitmentMapper recruitmentMapper;
    @Resource
    private TechnicianMapper technicianMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ITechnicianService technicianService;
    @Resource
    private BrokerMapper brokerMapper;
    @Resource
    private GaoDeUtil gaoDeUtil;

    /**
     * 查询技师申请
     *
     * @param id 技师申请主键
     * @return 技师申请
     */
    @Override
    public Recruitment selectRecruitmentById(Long id) {
        return recruitmentMapper.selectRecruitmentById(id);
    }

    /**
     * 查询技师申请列表
     *
     * @param recruitment 技师申请
     * @return 技师申请
     */
    @Override
    public List<Recruitment> selectRecruitmentList(Recruitment recruitment) {
        return recruitmentMapper.selectRecruitmentList(recruitment);
    }

    /**
     * 新增技师申请
     *
     * @param recruitment 技师申请
     * @return 结果
     */
    @Override
    public int insertRecruitment(Recruitment recruitment) {
        recruitment.setCreateTime(DateUtils.getNowDate());
        return recruitmentMapper.insertRecruitment(recruitment);
    }

    /**
     * 修改技师申请
     *
     * @param recruitment 技师申请
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateRecruitment(Recruitment recruitment) {
        Recruitment recruitment1 = recruitmentMapper.selectRecruitmentById(recruitment.getId());
        if (recruitment1.getAuditStatus() != 0) {
            throw new RuntimeException("该条数据已处理");
        }
        if (recruitment.getAuditStatus() == 1) {
            User user = userMapper.selectUserById(recruitment.getUserId());
            Technician technician = new Technician();
            technician.setUserId(recruitment.getUserId());
            technician.setMark(new BigDecimal(5));
            if (!CheckEmptyUtil.isEmpty(recruitment.getFrontPhoto())) {
                technician.setTechnicianAvatar(recruitment.getFrontPhoto());
            } else {
                technician.setTechnicianAvatar("");
            }
            technician.setTechnicianName(recruitment.getRecruitmentName());
            if ("男".equals(recruitment.getRecruitmentSex())) {
                technician.setTechnicianSex(0);
            } else {
                technician.setTechnicianSex(1);
            }
            Short year = IdcardUtil.getYearByIdCard(recruitment.getRecruitmentRealNum());
            technician.setTechnicianYear(Integer.parseInt(String.valueOf(year)));
            technician.setTechnicianAge(IdcardUtil.getAgeByIdCard(recruitment.getRecruitmentRealNum()));
            String longLat = gaoDeUtil.geoCode(recruitment.getWorkCity() + recruitment.getReceivingArea());
            technician.setLongitude(longLat.split(",")[0]);
            technician.setLatitude(longLat.split(",")[1]);
            WSSsdrAddress wsSsdrAddress = AddressUtil.addressFormat(recruitment.getWorkCity() + recruitment.getReceivingArea());
            technician.setServiceCity(!CheckEmptyUtil.isEmpty(wsSsdrAddress.getCity()) ? wsSsdrAddress.getCity() : wsSsdrAddress.getProvince());
            String currentLocation = gaoDeUtil.ReGeoCode(longLat);
            technician.setCurrentLocation(currentLocation);


            String allItem = technicianMapper.selectAllItem();
            technician.setServiceItem(allItem);
            technician.setOrderReceiving(0);
            technician.setTechnicianIntro("简介");
            technician.setIsOnline(0);
            technician.setIdCard(recruitment.getRecruitmentRealNum());
            technician.setTechnicianTel(recruitment.getRecruitmentTel());
            technician.setTechnicianInvitationCode(user.getUserInvitationCode());
            technician.setTechnicianInvitedCode(user.getUserInvitedCode());
            technician.setTechnicianPass(MD5Utils.MD5(recruitment.getRecruitmentTel().substring(recruitment.getRecruitmentTel().length() - 6)));
            if (CheckEmptyUtil.isEmpty(user.getUserInvitedCode())) {
                technician.setTechnicianSource(10);
            } else {
                Broker broker = brokerMapper.select(user.getUserInvitationCode());
                Technician technician1 = technicianMapper.select(user.getUserInvitationCode());

                if (!CheckEmptyUtil.isEmpty(broker)) {
                    technician.setTechnicianSource(12);
                }
                if (!CheckEmptyUtil.isEmpty(technician1) && CheckEmptyUtil.isEmpty(broker)) {
                    technician.setTechnicianSource(11);
                }
            }


            int insertTechnician = technicianMapper.insertTechnician(technician);

            if (insertTechnician <= 0) {
                throw new RuntimeException("技师添加失败");
            }

            technicianService.addTechnicianTime(technician.getId());
        }
        recruitment.setUpdateTime(DateUtils.getNowDate());
        return recruitmentMapper.updateRecruitment(recruitment);
    }

    /**
     * 批量删除技师申请
     *
     * @param ids 需要删除的技师申请主键
     * @return 结果
     */
    @Override
    public int deleteRecruitmentByIds(Long[] ids) {
        return recruitmentMapper.deleteRecruitmentByIds(ids);
    }

    /**
     * 删除技师申请信息
     *
     * @param id 技师申请主键
     * @return 结果
     */
    @Override
    public int deleteRecruitmentById(Long id) {
        return recruitmentMapper.deleteRecruitmentById(id);
    }
}
